package com.cade.agriculturemall.view.personalCenter;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.cade.agriculturemall.InfoContentActivity;
import com.cade.agriculturemall.R;
import com.cade.agriculturemall.base.BaseFragment;
import com.cade.agriculturemall.model.UserInfoEntity;
import com.cade.agriculturemall.view.login.LoginActivity;
import com.cade.agriculturemall.view.personalCenter.collect.CollectActivity;
import com.cade.agriculturemall.view.personalCenter.discount.DiscountActivity;
import com.cade.agriculturemall.view.personalCenter.help.HelpActivity;
import com.cade.agriculturemall.view.personalCenter.historyTrack.HistoryTrackActivity;
import com.cade.agriculturemall.view.personalCenter.manageAddress.ManageAddressActivity;
import com.cade.agriculturemall.view.personalCenter.member.MemberCenterActivity;
import com.cade.agriculturemall.view.personalCenter.ordersManage.MyOrdersActivity;
import com.cade.agriculturemall.view.personalCenter.personalInfo.PersonalInfoActivity;
import com.cade.agriculturemall.widget.DoubleListLayout;
import com.cade.agriculturemall.widget.TitleIntentLayout;

import de.hdodenhof.circleimageview.CircleImageView;
import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * Created by Lobby on 2017/11/16.
 */

public class PersonalCenterFragment extends BaseFragment<PersonalCenterContract.IPresenter> implements PersonalCenterContract.IView, View.OnClickListener {

    private DoubleListLayout doubleLayoutCollect;
    private DoubleListLayout doubleLayoutHistory;
    private UserInfoEntity mEntity = new UserInfoEntity();
    private int collectSize = 0;

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.getCollectSize();
        mEntity = mPresenter.loadPensonalData();
        fundation();
    }

    public void fundation() {

        doubleLayoutCollect = (DoubleListLayout) getView().findViewById(R.id.personal_fragment_doublistly_collect);
        doubleLayoutHistory = (DoubleListLayout) getView().findViewById(R.id.personal_fragment_doublistly_history);
       doubleLayoutCollect.setOnClickListener(this);
        doubleLayoutHistory.setOnClickListener(this);
        TitleIntentLayout mTITLayoutOrder = (TitleIntentLayout) getView().findViewById(R.id.personal_fragment_layouty_order);
        mTITLayoutOrder.setText("我的订单", "查看更多订单");
        Button btnNoPay = (Button) getView().findViewById(R.id.personal_fragment_order_no_pay_btn);
        Button btnNoDelivery = (Button) getView().findViewById(R.id.personal_fragment_order_no_delivery_btn);
        Button btnNoRecive = (Button) getView().findViewById(R.id.personal_fragment_order_no_recive_btn);
        Button btnNoCriticize = (Button) getView().findViewById(R.id.personal_fragment_order_no_criticize_btn);
        Button bntFollow = (Button) getView().findViewById(R.id.personal_fragment_order_follow_up_btn);
        Button btnTicket = (Button) getView().findViewById(R.id.personal_fragment_order_ticket_btn);
        Button btnMember = (Button) getView().findViewById(R.id.personal_fragment_order_member_btn);
        btnNoPay.setOnClickListener(this);
        btnNoRecive.setOnClickListener(this);
        btnNoDelivery.setOnClickListener(this);
        btnNoCriticize.setOnClickListener(this);
        bntFollow.setOnClickListener(this);
        btnTicket.setOnClickListener(this);
        btnMember.setOnClickListener(this);
        TitleIntentLayout mTIntentLayoutAddress = (TitleIntentLayout) getView().findViewById(R.id.personal_fragment_layouty_address);
        mTIntentLayoutAddress.setText("地址管理", "");
        mTIntentLayoutAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ManageAddressActivity.class));
            }
        });
        mTITLayoutOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyOrdersActivity.class));
            }
        });
        TitleIntentLayout mTIntentLayoutOurs = (TitleIntentLayout) getView().findViewById(R.id.personal_fragment_layouty_about_ours);
        mTIntentLayoutOurs.setText("关于我们", "");
        TitleIntentLayout mTIntentLayoutHelp = (TitleIntentLayout) getView().findViewById(R.id.personal_fragment_layouty_help);
        mTIntentLayoutHelp.setText("使用帮助", "");
        ImageView blurImage = (ImageView) getView().findViewById(R.id.personal_fragment_image_back);
        CircleImageView mAvatar = (CircleImageView) getView().findViewById(R.id.personal_fragment_image);

        if (mEntity != null) {
            if (mEntity.getLogo_url() != null) {
                Glide.with(this).load(mEntity.getLogo_url())
                        .bitmapTransform(new BlurTransformation(getActivity(), 25), new CenterCrop(getActivity()))
                        .into(blurImage);
                Glide.with(this).load(mEntity.getLogo_url())
                        .into(mAvatar);
            }
        } else {
            Glide.with(this).load(R.mipmap.login_back)
                    .bitmapTransform(new BlurTransformation(getActivity(), 25), new CenterCrop(getActivity()))
                    .into(blurImage);
            Glide.with(this).load(mEntity.getLogo_url())
                    .asBitmap()
                    .placeholder(R.mipmap.defaule_circle_grey_ava_36dp)
                    .into(mAvatar);
        }
        mAvatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), PersonalInfoActivity.class);
                startActivity(intent);
            }
        });

        mTIntentLayoutOurs.setOnClickListener(this);
        mTIntentLayoutHelp.setOnClickListener(this);

        TextView tvInfo= (TextView) getView().findViewById(R.id.personnal_info_text);
        tvInfo.setText("    "+mEntity.getNickname());
    }


    @Override
    public int setupLayout() {
        return R.layout.layout_personal_fragment;
    }

    @Override
    public void setupView() {

    }

    @Override
    public void setupEvent() {
        Button mBtnExit = (Button) getView().findViewById(R.id.personal_fragment_exit);
        mBtnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginOut();
            }
        });
    }

    @Override
    public void setupData() {
    }

    @Override
    public PersonalCenterContract.IPresenter setupPresenter() {
        return new PersonalPrensenter(this);
    }

    @Override
    public void onClick(View v) {
        doubLyOnclick(v);
    }

    @Override
    public void doubLyOnclick(View view) {
        Intent orderIntent = new Intent(getActivity(), MyOrdersActivity.class);
        switch (view.getId()) {
            case R.id.personal_fragment_doublistly_collect:
//                收藏页面
                Intent intentCollect = new Intent(getActivity(), CollectActivity.class);
                startActivity(intentCollect);
                break;
            case R.id.personal_fragment_doublistly_history:
//                浏览历史
                startActivity(new Intent(getActivity(), HistoryTrackActivity.class));
                break;
            case R.id.personal_fragment_layouty_order:
                break;
            case R.id.personal_fragment_order_no_pay_btn:
                orderIntent.putExtra("type", 1);
                startActivity(orderIntent);
                break;
            case R.id.personal_fragment_order_no_delivery_btn:
                orderIntent.putExtra("type", 2);
                startActivity(orderIntent);
                break;
            case R.id.personal_fragment_order_no_recive_btn:
                orderIntent.putExtra("type", 3);
                startActivity(orderIntent);
                break;
            case R.id.personal_fragment_order_no_criticize_btn:
                orderIntent.putExtra("type", 4);
                startActivity(orderIntent);
                break;
            case R.id.personal_fragment_order_follow_up_btn:
//                orderIntent.putExtra("type",1);
                break;
            case R.id.personal_fragment_order_ticket_btn:
                startActivity(new Intent(getActivity(), DiscountActivity.class));
                break;
            case R.id.personal_fragment_order_member_btn:
                startActivity(new Intent(getActivity(), MemberCenterActivity.class));
                break;
            case R.id.personal_fragment_layouty_about_ours:
                Intent intent = new Intent(getActivity(), InfoContentActivity.class);
                intent.putExtra("tittle", "关于我们");
                intent.putExtra("content", "about.txt");
                intent.putExtra("type", 1);
                startActivity(intent);
                break;
            case R.id.personal_fragment_layouty_help:
                startActivity(new Intent(getActivity(), HelpActivity.class));
                break;
        }

    }


    //退出登录
    @Override
    public void loginOut() {
        AVUser.getCurrentUser().logOut();
        Intent intent = new Intent(getActivity(), LoginActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @Override
    public void getInfo(UserInfoEntity entity) {
        mEntity = entity;
    }

    @Override
    public void getCollectSize(int size) {
        doubleLayoutCollect.setText(size + "", "收藏夹");
        doubleLayoutHistory.setText(size+"", "足迹");

    }

}
